﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain;
using Service.Interface;
using Clannan.Models.JsonModel;

namespace Clannan.Controllers
{
    public class PageController : Controller
    {
        public IAreaService AreaService;
        public IPageService PageService;

        /// <summary>
        /// 添加笔记本页
        /// </summary>
        /// <param name="areaID">分区分区ID</param>
        /// <param name="pageName">页名称</param>
        /// <param name="pageContent">页内容</param>
        /// <returns></returns>
        public JsonResult Create(string areaID, string pageName, string pageContent)
        {
            Page page = new Page();
            page.Area = new Area { ID = Convert.ToInt32(areaID) };
            page.Name = pageName;
            page.Content = pageContent;
            page.CreateTime = DateTime.Now;
            page.UpdateTime = DateTime.Now;
            page.MarkDelete = false;
            page.Share = false;
            page.ReplyCount = 0;
            this.PageService.SaveAndAddPageCount(page);

            return Json(new { Message = 1, PageID = page.ID });
        }

        /// <summary>
        /// 将页放入垃圾箱
        /// </summary>
        /// <param name="pageID">页ID</param>
        /// <returns></returns>
        public JsonResult Throw(string pageID)
        {
            this.PageService.UpdatePageMarkDelete(pageID, true);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 从垃圾箱中还原页
        /// </summary>
        /// <param name="pageID">页ID</param>
        /// <returns></returns>
        public JsonResult Restore(string pageID)
        {
            this.PageService.UpdatePageMarkDelete(pageID, false);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 更新页内容
        /// </summary>
        /// <param name="pageID">页ID</param>
        /// <param name="pageContent">页内容</param>
        /// <returns></returns>
        public JsonResult Update(string pageID, string pageContent)
        {
            int count = pageContent.Count();
            Page page = this.PageService.Get(pageID);
            page.Content = pageContent;
            page.UpdateTime = DateTime.Now;
            this.PageService.Update(page);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 重命名页
        /// </summary>
        /// <param name="pageID">页ID</param>
        /// <param name="pageName">页名称</param>
        /// <returns></returns>
        public JsonResult UpdateName(string pageID, string pageName)
        {
            Page page = this.PageService.Get(pageID);
            page.Name = pageName;
            this.PageService.Update(page);

            return Json(new { Message = 1 });
        }


        /// <summary>
        /// 更新页所属分区
        /// </summary>
        /// <param name="pageID">页ID</param>
        /// <param name="areaID">更新的分区ID</param>
        /// <returns></returns>
        public JsonResult UpdatePageArea(string pageID, string areaID)
        {
            Page page = this.PageService.Get(pageID);
            page.Area = new Area { ID = Convert.ToInt32(areaID) };
            this.PageService.Update(page);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 删除页
        /// </summary>
        /// <param name="pageID">页ID</param>
        /// <returns></returns>
        public JsonResult Delete(string pageID)
        {
            this.PageService.DeleteAndMinusPageCount(pageID);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 获取页内容
        /// </summary>
        /// <param name="pageid">页ID</param>
        /// <returns></returns>
        public JsonResult Get(string pageID)
        {
            Page page = this.PageService.Get(pageID);
            PageJsonModel pageJsonModel = new PageJsonModel();
            pageJsonModel = pageJsonModel.Copy(pageJsonModel, page);

            return Json(new { Message = 1, Content = page.Content });
        }

        /// <summary>
        /// 获取页的分页集合
        /// </summary>
        /// <param name="areaID">分区ID</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="sortType">排序类型</param>
        /// <returns></returns>
        public JsonResult GetAll(string areaID, string pageIndex, string pageSize, string sortType)
        {
            List<PageJsonModel> pageList = new List<PageJsonModel>();//Json返回页集合
            PageJsonModel pageJsonModel;//Json模型
            int pageCount;              //总页数

            if (sortType == null)
            {
                Area area = this.AreaService.Get(areaID);
                sortType = area.PageSortType;
            }
            //获取分页数据
            IList<Page> pageListData = this.PageService.LoadAllByPage(Convert.ToInt32(areaID), Convert.ToInt32(pageIndex), Convert.ToInt32(pageSize), out pageCount, sortType);
            //数据处理
            foreach (Page page in pageListData)
            {
                pageJsonModel = new PageJsonModel();
                pageJsonModel = pageJsonModel.Copy(pageJsonModel, page);
                pageList.Add(pageJsonModel);
            }

            return Json(new { Message = 1, PageList = pageList, PageCount = pageCount, SortType = sortType });
        }

        /// <summary>
        /// 共享页
        /// </summary>
        /// <param name="pageID">页ID</param>
        /// <returns></returns>
        public JsonResult Share(string pageID)
        {
            this.PageService.UpdatePageShare(pageID, true);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 取消共享页
        /// </summary>
        /// <param name="pageID">页ID</param>
        /// <returns></returns>
        public JsonResult CancelShare(string pageID)
        {
            this.PageService.UpdatePageShare(pageID, false);

            return Json(new { Message = 1 });
        }
    }
}
